package com.solution.pc.home.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.solution.admin.home.entity.SiteConfigItemEntity;
import com.solution.admin.home.entity.SiteModuleEntity;
import com.solution.admin.home.service.SiteConfigItemService;
import com.solution.admin.home.service.SiteModuleService;
import com.solution.common.constants.PublicStatus;
import com.solution.common.controller.BaseController;

/**
 * 网站指南页面
 * @author xkl
 * @date 2017年12月28日 下午7:24:28
 */
@RequestMapping("/pc/guide")
@Controller
public class GuideController extends BaseController {
	
	private Logger logger = LoggerFactory.getLogger(GuideController.class);
	
	@Autowired
	private SiteModuleService siteModuleService;
	
	@Autowired
	private SiteConfigItemService siteConfigItemService;
	
	/**
	 * 跳转网站指南
	 * @param request
	 * @return
	 * @author xkl
	 * @date 2017年12月29日 上午9:20:24
	 */
	@RequestMapping("/toWebGuide")
	public String toWebGuide(HttpServletRequest request){
		Long parentId = Long.parseLong(request.getParameter("parentId"));
		Long moduleId = Long.parseLong(request.getParameter("moduleId"));
		Long itemId = Long.parseLong(request.getParameter("itemId"));
		request.setAttribute("itemId", itemId);
		request.setAttribute("moduleId", moduleId);
		request.setAttribute("parentId", parentId);
		return "pc/guide/web_guide";
	}

	/**
	 * 获取左边栏
	 * @param request
	 * @return
	 * @author xkl
	 * @date 2017年12月29日 上午9:20:22
	 */
	@RequestMapping("getGuideLeft")
	@ResponseBody
	public Map<String, Object> getGuideLeft(HttpServletRequest request){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Long parentId = Long.parseLong(request.getParameter("parentId"));
		try {
			List<SiteModuleEntity> moduleList = siteModuleService.listSiteModuleByParentid(parentId);
			List<SiteConfigItemEntity> configItemList = siteConfigItemService.listSiteConfigItemByParentId(parentId);
			Map<String, List<SiteConfigItemEntity>> itemListMap = new HashMap<String, List<SiteConfigItemEntity>>();
			//将item组装为listmap
			for (SiteConfigItemEntity itemEntity : configItemList) {
				String moduleId = String.valueOf(itemEntity.getModuleId());
				if (itemListMap.containsKey(moduleId)) {
					itemListMap.get(moduleId).add(itemEntity);
				} else {
					List<SiteConfigItemEntity> itemList = new ArrayList<SiteConfigItemEntity>();
					itemList.add(itemEntity);
					itemListMap.put(moduleId, itemList);
				}
			}
			//组装module
			for (SiteModuleEntity moduleEntity : moduleList) {
				String moduleId = String.valueOf(moduleEntity.getId()); 
				if (itemListMap.containsKey(moduleId)) {
					moduleEntity.setSiteConfigItemList(itemListMap.get(moduleId));
				}
			}
			resultMap.put("moduleList", moduleList);
			resultMap.put(PublicStatus.RESULT_CODE, "1");
			resultMap.put(PublicStatus.RESULT_MSG, "操作成功");
		} catch (Exception e) {
			handleException("获取左边栏失败", logger, e);
			resultMap.put(PublicStatus.RESULT_CODE, "0");
			resultMap.put(PublicStatus.RESULT_MSG, "操作失败");
		}
		return resultMap;
	}

	/**
	 * 
	 * @param request
	 * @return
	 * @author xkl
	 * @date 2017年12月29日 上午10:57:35
	 */
	@RequestMapping("getGuideContent")
	@ResponseBody
	public Map<String, Object> getGuideContent(HttpServletRequest request){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Long itemId = Long.parseLong(request.getParameter("itemId"));
		try {
			SiteConfigItemEntity siteConfigItem = siteConfigItemService.getSiteConfigItemById(itemId);
			resultMap.put("siteConfigItem", siteConfigItem);
			resultMap.put(PublicStatus.RESULT_CODE, "1");
			resultMap.put(PublicStatus.RESULT_MSG, "操作成功");
		} catch (Exception e) {
			handleException("获取内容失败", logger, e);
			resultMap.put(PublicStatus.RESULT_CODE, "0");
			resultMap.put(PublicStatus.RESULT_MSG, "操作失败");
		}
		return resultMap;
	}
}
